package hhm.android.my

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.igexin.sdk.PushManager
import hhm.android.library.utils.DataStoreUtils
import hhm.android.library.utils.OnMultiClickListener
import siau.android.library.KeyString
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async
import siau.android.base.*
import siau.android.http.model.CidRequest

@Route(path = SETTING_ROUTER)
class SettingActivity : SBBaseActivity(), CoroutineScope by MainScope() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        setContentView(R.layout.activity_setting)
        initTitle(getString(R.string.setting))

        val start = registerForActivityResult(IntentIntActivityResultContact()) {
            if (it == 1024) {
                //清除cid
                if (!TextUtils.isEmpty(KeyString.token)) {
                    val cidRequest = CidRequest("")
                    HttpHelper().updateCid(cidRequest)?.subscribe({}, {})
                }
                async {
                    DataStoreUtils.writeStringToDataStore(DataStoreUtils.TOKEN, "")
//                    SBApplication.startCalculate.value = false
                    SBApplication.clearBlueToothData()
                }

                KeyString.token = ""
                SBApplication.refreshMainActivityUI = false
                SBApplication.becomeCreator = false
                SBApplication.communityChildId = -1
                PushManager.getInstance().unBindAlias(this@SettingActivity, SBApplication.userData.userAlias, true)
                ARouter.getInstance().build(LOGIN_ROUTER).withFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK).navigation()
            }
        }

        findViewById<LinearLayout>(R.id.activity_setting_account_and_safe).setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                start.launch(Intent(this@SettingActivity, AccountAndSafeActivity::class.java))
            }
        })

        findViewById<LinearLayout>(R.id.activity_setting_class_control).setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                startActivity(Intent(this@SettingActivity, ClassControlActivity::class.java))
            }
        })

//        findViewById<LinearLayout>(R.id.activity_setting_privacy).setOnClickListener(object : OnMultiClickListener() {
//            override fun onMultiClick(v: View?) {
//                startActivity(Intent(this@SettingActivity, PrivacyActivity::class.java))
//            }
//        })

        findViewById<LinearLayout>(R.id.activity_setting_about_us).setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                startActivity(Intent(this@SettingActivity, AboutUsActivity::class.java))
            }
        })
        findViewById<LinearLayout>(R.id.activity_setting_help).setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                ARouter.getInstance().build(WEBVIEW_ROUTER).withString(KeyString.url, SBApplication.qaString).navigation()
            }
        })

        findViewById<TextView>(R.id.activity_setting_quit).setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                getSureDialog(this@SettingActivity, {
                    //清除cid
                    if (!TextUtils.isEmpty(KeyString.token)) {
                        val cidRequest = CidRequest("")
                        HttpHelper().updateCid(cidRequest)?.subscribe({}, {})
                    }
                    async {
                        DataStoreUtils.writeStringToDataStore(DataStoreUtils.TOKEN, "")
//                        SBApplication.startCalculate.value = false
                        SBApplication.clearBlueToothData()
                    }

                    KeyString.token = ""
                    SBApplication.refreshMainActivityUI = false
                    SBApplication.becomeCreator = false
                    SBApplication.communityChildId = -1
                    PushManager.getInstance().unBindAlias(this@SettingActivity, SBApplication.userData.userAlias, true)
                    ARouter.getInstance().build(LOGIN_ROUTER).withFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK).navigation()
                }, null, "", getString(R.string.sure_quit_login), false)
            }
        })
    }
}